package com.zjdiepu.www.manage.api.cw;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.AddedStatus1;
import com.zjdiepu.www.base.enums.status.BusiStatus;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.RemitAdjustStatus;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.enums.type.SortFieldType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.common.util.ZhppDateUtil;
import com.zjdiepu.www.ibiz.base.IDpBaseDealerInfoService;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.cw.ICwRemitAdjustBService;
import com.zjdiepu.www.ibiz.cw.ICwRemitAdjustbSubService;
import com.zjdiepu.www.ibiz.system.IBaseBusiUnitService;
import com.zjdiepu.www.ibiz.system.IBaseSalersmanService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessLoanService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.ibiz.yw.IYwFeesService;
import com.zjdiepu.www.model.base.dto.DpBaseDealerInfo;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.cw.dto.CwRemitAdjustA;
import com.zjdiepu.www.model.cw.dto.CwRemitAdjustB;
import com.zjdiepu.www.model.cw.dto.CwRemitAdjustbSub;
import com.zjdiepu.www.model.cw.ext.CwRemitAdjustBExt;
import com.zjdiepu.www.model.fk.dto.FkAuditList;
import com.zjdiepu.www.model.system.dto.BaseBusiUnit;
import com.zjdiepu.www.model.system.dto.BaseSalersman;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoan;
import com.zjdiepu.www.model.yw.dto.YwFees;
import com.zjdiepu.www.model.yw.ext.YwBusinessLoanExt;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;
import com.zjdiepu.www.util.YwExtHelper;

/**
 * Controller层<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年08月02日 15时40分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "CwRemitAdjustBController", produces="application/json;charset=UTF-8")
@Api(tags = {""}, produces="application/json;charset=UTF-8")
public class CwRemitAdjustBController extends BaseController {
    @Resource
    private ICwRemitAdjustBService cwRemitAdjustBService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;
    @Resource
    private IYwBusinessLoanService ywBusinessLoanService;
    @Resource
    private IYwFeesService ywFeesService;
    @Resource
    private IBaseSalersmanService baseSalersmanService;
    @Resource
    private IBaseBusiUnitService baseBusiUnitService;
    @Resource
    private IDpBaseDealerInfoService baseDealerInfoService;
    @Resource
    private ICwRemitAdjustbSubService cwRemitAdjustbSubService;
    /**
     * @author zhangzhengjian <br/>
     * @Title: save  <br/>
     * @Description: 新增 <br/>
     * @param cwRemitAdjustB
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute CwRemitAdjustB cwRemitAdjustB) {
    	AbstractSysUser currentUser = getCurrentUser();
    	cwRemitAdjustB.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	cwRemitAdjustB.setModifierName(currentUser.getName());
    	cwRemitAdjustB.setRemitAdjustStatus(RemitAdjustStatus.YI_TIAO_ZHENG.getCode());
        cwRemitAdjustBService.saveUpdateAndDelete(cwRemitAdjustB, OperateType.add);
        
        return ApiResponse.success(true);
    }
    @ApiOperation(value = "新增", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public ApiResponse insert(@Validated(Add.class) @ModelAttribute CwRemitAdjustB cwRemitAdjustB) {
    	AbstractSysUser currentUser = getCurrentUser();
    	String busiId=cwRemitAdjustB.getBusiId();
        
        //往待审批列表中插入数据
        FkAuditList fkAuditList= new FkAuditList();
    	fkAuditList.setBusiId(busiId);
    	fkAuditList.setModeId(ModeIDType.DKTZBSH.getCode());
    	fkAuditList.setSource("申请垫资调整B");
        YwBusiness ywbusiness=ywBusinessService.selectById(busiId);
    	if(ywbusiness!=null){
    		fkAuditList.setBusiConfirmTime(ywbusiness.getSubmitTime());
    		fkAuditList.setLoanAmount(ywbusiness.getRealLoanAmount());
    		fkAuditList.setChannelType(ywbusiness.getChannelType());
    		cwRemitAdjustB.setChannelType(ywbusiness.getChannelType());
    		cwRemitAdjustB.setLoanAmount(ywbusiness.getRealLoanAmount());
    		fkAuditList.setChannelTypeName(cachedDictionaryService.getName(String.valueOf(fkAuditList.getChannelType()), DataDicType.CHANNEL_TYPE));
    		cwRemitAdjustB.setChannelTypeName(cachedDictionaryService.getName(String.valueOf(fkAuditList.getChannelType()), DataDicType.CHANNEL_TYPE));
    		fkAuditList.setChannelStatus(ywbusiness.getChannelStatus());
    		fkAuditList.setChannelStatusName(cachedDictionaryService.getName(String.valueOf(fkAuditList.getChannelStatus()), DataDicType.CHANNEL_STATUS));
    		fkAuditList.setDealer(ywbusiness.getDealerId());
			BaseSalersman baseSalersman=baseSalersmanService.selectById(ywbusiness.getSalesmanId());
			if(baseSalersman!=null && baseSalersman.getUnitId()!=null){
				fkAuditList.setSalersmanName(baseSalersman.getName());
				cwRemitAdjustB.setSalersmanName(baseSalersman.getName());
				BaseBusiUnit baseBusiUnit=baseBusiUnitService.selectById(baseSalersman.getUnitId());
				if(baseBusiUnit !=null){
					fkAuditList.setDepartmentName(baseBusiUnit.getOrgName());
					cwRemitAdjustB.setDepartmentName(baseBusiUnit.getOrgName());
				}
			}
    		DpBaseDealerInfo baseDealerInfo=baseDealerInfoService.selectById(ywbusiness.getDealerId());
    		if(baseDealerInfo!=null){
    			fkAuditList.setDealer(baseDealerInfo.getDealerName());
    			cwRemitAdjustB.setDealer(baseDealerInfo.getDealerName());
    			cwRemitAdjustB.setDealerLevel(baseDealerInfo.getDealerLevel());
    		}
    		fkAuditList.setCusName(ywbusiness.getCustomerName());
    		fkAuditList.setCusIdno(ywbusiness.getCustomerNo());
    		fkAuditList.setBankname(cachedDictionaryService.getName(ywbusiness.getBankLoan(),DataDicType.LOAN_BANK));
    		cwRemitAdjustB.setCusName(ywbusiness.getCustomerName());
    		cwRemitAdjustB.setCusIdno(ywbusiness.getCustomerNo());
    		cwRemitAdjustB.setBankname(cachedDictionaryService.getName(ywbusiness.getBankLoan(),DataDicType.LOAN_BANK));
    		fkAuditList.setStatusList(1);
    	}
//    	fkAuditListService.saveUpdateAndDelete(fkAuditList, OperateType.add);
    	
    	cwRemitAdjustB.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	cwRemitAdjustB.setModifierName(currentUser.getName());
    	cwRemitAdjustB.setRemitAdjustStatus(RemitAdjustStatus.YI_SHEN_QING.getCode());
    	cwRemitAdjustB.initInsertData(cwRemitAdjustB.getModifier(), cwRemitAdjustB.getModifierName());
    	
    	//保存到垫款调整B子表
    	String justbId=cwRemitAdjustB.getId();
    	CwRemitAdjustbSub cwRemitAdjustbSub=new CwRemitAdjustbSub();
    	cwRemitAdjustbSub.setBusiId(busiId);
    	cwRemitAdjustbSub.setCusId(cwRemitAdjustB.getCusId());
    	cwRemitAdjustbSub.setRemitAdjustbId(justbId);
    	cwRemitAdjustbSub.setCarPriceNew(cwRemitAdjustB.getCarPriceNew());
    	cwRemitAdjustbSub.setLoanMonthNew(cwRemitAdjustB.getLoanMonthNew());
    	cwRemitAdjustbSub.setAmountMonthlyNew(cwRemitAdjustB.getAmountMonthlyNew());
    	cwRemitAdjustbSub.setTotalRepaymentAmountNew(cwRemitAdjustB.getTotalRepaymentAmountNew());
    	cwRemitAdjustbSub.setTotalRepaymentProportionNew(cwRemitAdjustB.getTotalRepaymentProportionNew());
    	cwRemitAdjustbSub.setMonthlyPrincipalAmountNew(cwRemitAdjustB.getMonthlyPrincipalAmountNew());
    	cwRemitAdjustbSub.setMonthlyFeeNew(cwRemitAdjustB.getMonthlyFeeNew());
    	cwRemitAdjustbSub.setFirstMonthAmountNew(cwRemitAdjustB.getFirstMonthAmountNew());
    	cwRemitAdjustbSub.setFirstMonthFeeNew(cwRemitAdjustB.getFirstMonthFeeNew());
    	cwRemitAdjustbSub.setFirstMonthTotalAmountNew(cwRemitAdjustB.getFirstMonthTotalAmountNew());
    	cwRemitAdjustbSub.setCarId(cwRemitAdjustB.getCarId());
    	cwRemitAdjustbSub.setRemarks(cwRemitAdjustB.getRemarks());
    	
    	cwRemitAdjustbSub.setLoanAmountOld(cwRemitAdjustB.getLoanAmountOld());
    	cwRemitAdjustbSub.setLoanAmountNew(cwRemitAdjustB.getLoanAmountNew());
    	cwRemitAdjustbSub.setLoanAmountAdjust(cwRemitAdjustB.getLoanAmountAdjust());
    	cwRemitAdjustbSub.setSignLoanAmountOld(cwRemitAdjustB.getSignLoanAmountOld());
    	cwRemitAdjustbSub.setSignLoanAmountNew(cwRemitAdjustB.getSignLoanAmountNew());
    	cwRemitAdjustbSub.setSignLoanAmountAdjust(cwRemitAdjustB.getSignLoanAmountAdjust());
    	
    	cwRemitAdjustbSub.setRebateAreaOld(cwRemitAdjustB.getRebateAreaOld());
    	cwRemitAdjustbSub.setRebateAreaNew(cwRemitAdjustB.getRebateAreaNew());
    	cwRemitAdjustbSub.setRebateDealerOld(cwRemitAdjustB.getRebateDealerOld());
    	cwRemitAdjustbSub.setRebateDealerNew(cwRemitAdjustB.getRebateDealerNew());
    	cwRemitAdjustbSub.setRebateProvinceNew(cwRemitAdjustB.getRebateProvinceNew());
    	cwRemitAdjustbSub.setRebateProvinceOld(cwRemitAdjustB.getRebateProvinceOld());
    	cwRemitAdjustbSub.setCarEstimatePriceNew(cwRemitAdjustB.getCarEstimatePriceNew());
    	cwRemitAdjustbSub.setServicePriceNew(cwRemitAdjustB.getServicePriceNew());
    	
    	YwBusinessLoanExt ywBusinessLoanExt = new YwBusinessLoanExt();
		ywBusinessLoanExt.setBusiId(busiId);
		ywBusinessLoanExt.setCarId(cwRemitAdjustB.getCarId());
		ywBusinessLoanExt.setBaseWhere();
		ywBusinessLoanExt.setRows(1);
		List<YwBusinessLoan> oldYwBusinessLoans = ywBusinessLoanService.selectByExt(ywBusinessLoanExt);
		if(oldYwBusinessLoans!=null && oldYwBusinessLoans.size()>0){
			YwBusinessLoan ywBusinessLoanOld= new YwBusinessLoan();
			cwRemitAdjustbSub.setAmountMonthlyOld(ywBusinessLoanOld.getAmountMonthly());
			cwRemitAdjustbSub.setCarEstimatePriceOld(ywBusinessLoanOld.getCarEstimatePrice());
			cwRemitAdjustbSub.setCarPriceOld(ywBusinessLoanOld.getCarPrice());
			cwRemitAdjustbSub.setFirstMonthAmountOld(ywBusinessLoanOld.getFirstMonthAmount());
			cwRemitAdjustbSub.setFirstMonthFee2Old(ywBusinessLoanOld.getFirstMonthFee2());
			cwRemitAdjustbSub.setFirstMonthFeeOld(ywBusinessLoanOld.getFirstMonthFee());
			cwRemitAdjustbSub.setFirstMonthTotalAmountOld(ywBusinessLoanOld.getFirstMonthTotalAmount());
			cwRemitAdjustbSub.setTotalRepaymentAmountOld(ywBusinessLoanOld.getTotalRepaymentAmount());
			cwRemitAdjustbSub.setTotalRepaymentProportionOld(ywBusinessLoanOld.getTotalRepaymentProportion());
			cwRemitAdjustbSub.setMonthlyPrincipalAmountOld(ywBusinessLoanOld.getMonthlyPrincipalAmount());
			cwRemitAdjustbSub.setMonthlyFeeOld(ywBusinessLoanOld.getMonthlyFee());
			cwRemitAdjustbSub.setMonthlyFee2Old(ywBusinessLoanOld.getMonthlyFee2());
			cwRemitAdjustbSub.setLoanMonthOld(ywBusinessLoanOld.getLoanMonth());
			cwRemitAdjustbSub.setServicePriceOld(ywBusinessLoanOld.getServicePrice()==null?0:ywBusinessLoanOld.getServicePrice().doubleValue());
		}
//    	cwRemitAdjustbSubService.saveUpdateAndDelete(cwRemitAdjustbSub, OperateType.add);
    	
    	//保存垫款调整B表
//        cwRemitAdjustBService.saveUpdateAndDelete(cwRemitAdjustB, OperateType.add);
        
        cwRemitAdjustBService.saveAll(cwRemitAdjustB,cwRemitAdjustbSub,fkAuditList);
        
        return ApiResponse.success(true);
    }
    @ApiOperation(value = "修改", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public ApiResponse update(@Validated(Update.class) @ModelAttribute CwRemitAdjustB cwRemitAdjustB) {
    	AbstractSysUser currentUser = getCurrentUser();
    	String busiId=cwRemitAdjustB.getBusiId();
    	cwRemitAdjustB.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	cwRemitAdjustB.setModifierName(currentUser.getName());
    	cwRemitAdjustB.setModified((int)(System.currentTimeMillis() / 1000));
    	cwRemitAdjustB.setRemitAdjustStatus(RemitAdjustStatus.YI_SHEN_QING.getCode());
//        cwRemitAdjustBService.saveUpdateAndDelete(cwRemitAdjustB, OperateType.edit);
        
        //修改到垫款调整B子表
        //获取垫款调整子表ID
    	String justbSubId=cwRemitAdjustB.getRemitAdjustbId();
    	CwRemitAdjustbSub cwRemitAdjustbSub=new CwRemitAdjustbSub();
    	
    	cwRemitAdjustbSub.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	cwRemitAdjustbSub.setModifierName(currentUser.getName());
    	cwRemitAdjustbSub.setModified((int)(System.currentTimeMillis() / 1000));
    	cwRemitAdjustbSub.setId(justbSubId);
    	cwRemitAdjustbSub.setBusiId(busiId);
    	cwRemitAdjustbSub.setCusId(cwRemitAdjustB.getCusId());
    	cwRemitAdjustbSub.setRemitAdjustbId(cwRemitAdjustB.getId());
    	cwRemitAdjustbSub.setCarPriceNew(cwRemitAdjustB.getCarPriceNew());
    	cwRemitAdjustbSub.setLoanMonthNew(cwRemitAdjustB.getLoanMonthNew());
    	cwRemitAdjustbSub.setAmountMonthlyNew(cwRemitAdjustB.getAmountMonthlyNew());
    	cwRemitAdjustbSub.setTotalRepaymentAmountNew(cwRemitAdjustB.getTotalRepaymentAmountNew());
    	cwRemitAdjustbSub.setTotalRepaymentProportionNew(cwRemitAdjustB.getTotalRepaymentProportionNew());
    	cwRemitAdjustbSub.setMonthlyPrincipalAmountNew(cwRemitAdjustB.getMonthlyPrincipalAmountNew());
    	cwRemitAdjustbSub.setMonthlyFeeNew(cwRemitAdjustB.getMonthlyFeeNew());
    	cwRemitAdjustbSub.setFirstMonthAmountNew(cwRemitAdjustB.getFirstMonthAmountNew());
    	cwRemitAdjustbSub.setFirstMonthFeeNew(cwRemitAdjustB.getFirstMonthFeeNew());
    	cwRemitAdjustbSub.setFirstMonthTotalAmountNew(cwRemitAdjustB.getFirstMonthTotalAmountNew());
    	cwRemitAdjustbSub.setCarId(cwRemitAdjustB.getCarId());
    	cwRemitAdjustbSub.setRemarks(cwRemitAdjustB.getRemarks());
    	
    	cwRemitAdjustbSub.setLoanAmountOld(cwRemitAdjustB.getLoanAmountOld());
    	cwRemitAdjustbSub.setLoanAmountNew(cwRemitAdjustB.getLoanAmountNew());
    	cwRemitAdjustbSub.setLoanAmountAdjust(cwRemitAdjustB.getLoanAmountAdjust());
    	cwRemitAdjustbSub.setSignLoanAmountOld(cwRemitAdjustB.getSignLoanAmountOld());
    	cwRemitAdjustbSub.setSignLoanAmountNew(cwRemitAdjustB.getSignLoanAmountNew());
    	cwRemitAdjustbSub.setSignLoanAmountAdjust(cwRemitAdjustB.getSignLoanAmountAdjust());
    	cwRemitAdjustbSub.setServicePriceNew(cwRemitAdjustB.getServicePriceNew());
    	
    	cwRemitAdjustbSub.setRebateAreaOld(cwRemitAdjustB.getRebateAreaOld());
    	cwRemitAdjustbSub.setRebateAreaNew(cwRemitAdjustB.getRebateAreaNew());
    	cwRemitAdjustbSub.setRebateDealerOld(cwRemitAdjustB.getRebateDealerOld());
    	cwRemitAdjustbSub.setRebateDealerNew(cwRemitAdjustB.getRebateDealerNew());
    	cwRemitAdjustbSub.setRebateProvinceNew(cwRemitAdjustB.getRebateProvinceNew());
    	cwRemitAdjustbSub.setRebateProvinceOld(cwRemitAdjustB.getRebateProvinceOld());
    	cwRemitAdjustbSub.setCarEstimatePriceNew(cwRemitAdjustB.getCarEstimatePriceNew());
    	
    	YwBusinessLoanExt ywBusinessLoanExt = new YwBusinessLoanExt();
		ywBusinessLoanExt.setBusiId(busiId);
		ywBusinessLoanExt.setCarId(cwRemitAdjustB.getCarId());
		ywBusinessLoanExt.setBaseWhere();
		ywBusinessLoanExt.setRows(1);
		List<YwBusinessLoan> oldYwBusinessLoans = ywBusinessLoanService.selectByExt(ywBusinessLoanExt);
		if(oldYwBusinessLoans!=null && oldYwBusinessLoans.size()>0){
			YwBusinessLoan ywBusinessLoanOld= oldYwBusinessLoans.get(0);
			cwRemitAdjustbSub.setAmountMonthlyOld(ywBusinessLoanOld.getAmountMonthly());
			cwRemitAdjustbSub.setCarEstimatePriceOld(ywBusinessLoanOld.getCarEstimatePrice());
			cwRemitAdjustbSub.setCarPriceOld(ywBusinessLoanOld.getCarPrice());
			cwRemitAdjustbSub.setFirstMonthAmountOld(ywBusinessLoanOld.getFirstMonthAmount());
			cwRemitAdjustbSub.setFirstMonthFee2Old(ywBusinessLoanOld.getFirstMonthFee2());
			cwRemitAdjustbSub.setFirstMonthFeeOld(ywBusinessLoanOld.getFirstMonthFee());
			cwRemitAdjustbSub.setFirstMonthTotalAmountOld(ywBusinessLoanOld.getFirstMonthTotalAmount());
			cwRemitAdjustbSub.setTotalRepaymentAmountOld(ywBusinessLoanOld.getTotalRepaymentAmount());
			cwRemitAdjustbSub.setTotalRepaymentProportionOld(ywBusinessLoanOld.getTotalRepaymentProportion());
			cwRemitAdjustbSub.setMonthlyPrincipalAmountOld(ywBusinessLoanOld.getMonthlyPrincipalAmount());
			cwRemitAdjustbSub.setMonthlyFeeOld(ywBusinessLoanOld.getMonthlyFee());
			cwRemitAdjustbSub.setMonthlyFee2Old(ywBusinessLoanOld.getMonthlyFee2());
			cwRemitAdjustbSub.setLoanMonthOld(ywBusinessLoanOld.getLoanMonth());
			cwRemitAdjustbSub.setServicePriceOld(ywBusinessLoanOld.getServicePrice()==null?0:ywBusinessLoanOld.getServicePrice().doubleValue());
		}
//    	cwRemitAdjustbSubService.saveUpdateAndDelete(cwRemitAdjustbSub, OperateType.edit);
		cwRemitAdjustBService.updateAll(cwRemitAdjustB,cwRemitAdjustbSub);
        return ApiResponse.success(true);
    }
    /**
     * @author zhangzhengjian <br/>
     * @Title: remove  <br/>
     * @Description: 删除<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	cwRemitAdjustBService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: disable  <br/>
     * @Description: 禁用<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	cwRemitAdjustBService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: enable <br/>
     * @Description: 启用<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	cwRemitAdjustBService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: modify  <br/>
     * @Description: 修改 <br/>
     * @param cwRemitAdjustB
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改", response = Boolean.class, httpMethod="POST", tags={"修改(此方法暂时不用)"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute CwRemitAdjustB cwRemitAdjustB) {
    	String busiId=cwRemitAdjustB.getBusiId();
    	AbstractSysUser currentUser = getCurrentUser();
    	cwRemitAdjustB.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	cwRemitAdjustB.setModifierName(currentUser.getName());
    	cwRemitAdjustB.setRemitAdjustStatus(RemitAdjustStatus.YI_TIAO_ZHENG.getCode());
    	cwRemitAdjustBService.saveUpdateAndDelete(cwRemitAdjustB, OperateType.edit);
        
        //更新贷款信息表数据
        YwBusinessLoan ywBusinessLoan= new YwBusinessLoan();
        ywBusinessLoan.setBusiId(busiId);
        YwBusinessLoan ywBusinessLoan1=ywBusinessLoanService.selectRefined(ywBusinessLoan);
        ywBusinessLoan1.setLoanAmount(cwRemitAdjustB.getLoanAmountNew());
        ywBusinessLoan1.setSignLoanAmount(cwRemitAdjustB.getSignLoanAmountNew());
        ywBusinessLoanService.saveUpdateAndDelete(ywBusinessLoan1, OperateType.edit);
        
        //更新按揭日报 表数据
        YwFees ywfees= new YwFees();
        ywfees.setBusiId(busiId);
        YwFees ywfees1=ywFeesService.selectRefined(ywfees);
        ywfees1.setRebateDealer(cwRemitAdjustB.getRebateDealerNew());
        ywfees1.setRebateProvince(cwRemitAdjustB.getRebateProvinceNew());
        ywfees1.setRebateArea(cwRemitAdjustB.getRebateAreaNew());
        ywfees1.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
        ywfees1.setModifierName(currentUser.getName());
        ywFeesService.saveUpdateAndDelete(ywfees1, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询", response = CwRemitAdjustB.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       CwRemitAdjustB cwRemitAdjustB = cwRemitAdjustBService.selectById(id);
       if(null == cwRemitAdjustB) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(cwRemitAdjustB);
    }
    @ApiOperation(value = "精确查询",response = CwRemitAdjustA.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "selectRefined", method = RequestMethod.POST)
    public ApiResponse selectRefined(@ModelAttribute CwRemitAdjustB cwRemitAdjustB){
    	cwRemitAdjustB.setOrderSql(" created desc");
    	CwRemitAdjustB cwRemitAdjust=cwRemitAdjustBService.selectRefined(cwRemitAdjustB);
    	if(cwRemitAdjust!=null){
    		cwRemitAdjust.setJbTime(ZhppDateUtil.stampToDate(cwRemitAdjust.getModified(), "yyyy-MM-dd"));
    		return ApiResponse.success(cwRemitAdjust);
    	}
    	return ApiResponse.success(cwRemitAdjust);
    }
    /**
     * @author zhangzhengjian <br/>
     * @Title: list  <br/>
     * @Description: 条件查询 <br/>
     * @param cwRemitAdjustBExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询",response = CwRemitAdjustB.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute CwRemitAdjustBExt cwRemitAdjustBExt){
        cwRemitAdjustBExt.notDeleted();
        PageVo page = getPage(cwRemitAdjustBExt);
        cwRemitAdjustBExt.setRemitAdjustStatus(RemitAdjustStatus.YI_SHEN_HE.getCode());
        int records = cwRemitAdjustBService.countByExt(cwRemitAdjustBExt);
        page.setRecords(records);
        if (records > 0) {
            List<CwRemitAdjustB> dpBaseDataDictionaries = cwRemitAdjustBService.selectByExt(cwRemitAdjustBExt);
            page.setRows(dpBaseDataDictionaries);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    @ApiOperation(value = "条件查询业务信息",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "multiList", method = RequestMethod.POST)
    public ApiResponse multiList(@Validated @ModelAttribute CwRemitAdjustBExt cwRemitAdjustBExt){
    	 cwRemitAdjustBExt.notDeleted();
    	 cwRemitAdjustBExt.setRemitAdjustStatus(RemitAdjustStatus.YI_SHEN_HE.getCode());
         PageVo page = getPage(cwRemitAdjustBExt);
         int records = cwRemitAdjustBService.countByExt(cwRemitAdjustBExt);
         page.setRecords(records);
         if (records > 0) {
             List<CwRemitAdjustB> dpBaseDataDictionaries = cwRemitAdjustBService.selectByExt(cwRemitAdjustBExt);
             page.setRows(dpBaseDataDictionaries);
         } else {
             page.setRows(CollectionInitUtil.emptyList());
         }
        return ApiResponse.success(page);
	}
    
    
    @ApiOperation(value = "条件查询业务信息",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "selectMultiList", method = RequestMethod.POST)
    public ApiResponse selectMultiList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser currentUser =(SysUser)getCurrentUser();
        PageVo page = getPage(ywBusinessQueryRequest);
        YwExtHelper.initExt(ywBusinessQueryRequest, SortFieldType.SUBMIT_TIME);
        ywBusinessQueryRequest.setBaseWhere();
        ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
        ywBusinessQueryRequest.setCodeUnits( currentUser.getCodeUnits());
        ywBusinessQueryRequest.setAddedStatus1Less(AddedStatus1.CAI_WU_YI_SHEN_HE_ZHONG.getCode());
        ywBusinessQueryRequest.setBusiStatusNqMore(BusiStatus.YIN_HANG_YI_FANG_KUAN.getCode());
        ywBusinessQueryRequest.setCodeUnits(currentUser.getCodeUnits());
//        ywBusinessQueryRequest.setWhereSql(" and B.added_status1 >= 100 ");
        int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesssList = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
            CwRemitAdjustB cwRemitAdjustB1= null;
            for (YwBusiness ywBusiness : ywBusinesssList) {
            	cwRemitAdjustB1=new CwRemitAdjustB();
            	cwRemitAdjustB1.setBusiId(ywBusiness.getId());
            	CwRemitAdjustB cwRemitAdjustB=cwRemitAdjustBService.selectRefined(cwRemitAdjustB1);
            	if(cwRemitAdjustB!=null){
            		ywBusiness.setRemitAdjustStatus(cwRemitAdjustB.getRemitAdjustStatus());
            	}
            	ywBusiness.setChannelTypeDisplay(cachedDictionaryService.getName(ywBusiness.getChannelType(), DataDicType.CHANNEL_TYPE));
            	ywBusiness.setBankLoanDisplay(cachedDictionaryService.getName(String.valueOf(ywBusiness.getBankLoan()), DataDicType.LOAN_BANK));
            	cwRemitAdjustB=null;
			}
            page.setRows(ywBusinesssList);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
}
